package pt.uma.gap.accessperm;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;

import pt.uma.gap.graph.MethodNameException;

public class Utilities {

	public static String MEM_TAG = "_Mem";
	public static String NEW_MEM_TAG = "_NewMem";
	public static String FIELD_TAG = "_";

	private static Logger logger = Logger.getLogger(Utilities.class.getName());

	/**
	 * Build invoked method complete name
	 * 
	 * @param methodInvocation
	 * @return Method complete name
	 * @throws Exception
	 */
	public static String getPathName(MethodInvocation methodInvocation)
			throws MethodNameException {
		String name = null;
		if (methodInvocation.resolveMethodBinding() != null) {
			for (ITypeBinding typeBinding : methodInvocation
					.resolveMethodBinding().getParameterTypes()) {
				if (name == null) {
					name = typeBinding.getName();
				} else {
					name += "," + typeBinding.getName();
				}
			}
			ITypeBinding declaringTypeBinding = methodInvocation
					.resolveMethodBinding().getDeclaringClass();

			name = declaringTypeBinding.getQualifiedName() + "."
					+ methodInvocation.getName() + "(" + name + ")";
		} else {
			logger.log(Level.WARNING,
					"methodInvocation.resolveMethodBinding() == null");
			throw new MethodNameException("Method Invocation name not found. "
					+ "Reason: methodInvocation.resolveMethodBinding() == null");
		}
		return name;
	}
}
